<?php

class AppAction extends Action {

	 /**
     * APP启动图片新增/修改
     * @param int $id   启动图片id
     * @param string $enableTime 启用时间
     * @param string $pictureUrl 图片url
     * @param string $residenceTime 停留时间
     * @param string $clickJumpUrl 点击跳转url
     * @param string $appHomePage app首页
     */
	public function updateSpalash($id='',$enableTime,$pictureUrl,$residenceTime,$clickJumpUrl='',$appHomePage){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_spalash = M('app_spalash');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        $time =  date('Y-m-d H:i:s',time());
        if ($id){
        	$update_spalash['enableTime'] = $enableTime;
        	$update_spalash['pictureUrl'] = $pictureUrl;
        	$update_spalash['residenceTime'] = $residenceTime;
        	$update_spalash['clickJumpUrl'] = $clickJumpUrl;
        	$update_spalash['appHomePage'] = $appHomePage;
        	$update_spalash['updateTime'] = $time;
        	$update_result = $app_spalash->where("id=$id")->save($update_spalash);
        	/** 4.  判断编辑是否成功 */        
        	if ($update_result !== false) {
        		$return['code'] = 0;
	            $return['msg'] = '编辑成功';
	            $return['data'] = [];
	            Output::instance()->object($return);
        	}else{
        		$return['code'] = 1;
	            $return['msg'] = '编辑失败请重试';
	            $return['data'] = [];
	            Output::instance()->object($return);
        	}
        	
        }else{
            //查询最大的启动时间点,以及将此时间以及输入时间转化为时间戳的形式进行比较
            $where_spalash_cid['cid'] = $customerId;
            $where_spalash_cid['deleted'] = 0;

            $bigTime = $app_spalash->field('enableTime')->where($where_spalash_cid)->order('id desc')->find();
            $bigTime = strtotime($bigTime['enableTime']);
            $inTime = strtotime($enableTime);

            if ($bigTime >= $inTime){
                $return['code'] = 1;
                $return['msg'] = '启动时间应大于现有欢迎图片启动时间';
                $return['data'] = [];
                Output::instance()->object($return);
                return;
            }

        	$add_spalash['cid'] = $customerId;
        	$add_spalash['enableTime'] = $enableTime;
        	$add_spalash['pictureUrl'] = $pictureUrl;
        	$add_spalash['residenceTime'] = $residenceTime;
        	$add_spalash['clickJumpUrl'] = $clickJumpUrl;
        	$add_spalash['appHomePage'] = $appHomePage;
        	$add_spalash['createTime'] = $time;
        	$add_spalash['updateTime'] = $time;
        	$add_result = $app_spalash->add($add_spalash);

        	/** 4.  判断添加是否成功 */
        	if ($add_result !== false) {
        		$return['code'] = 0;
	            $return['msg'] = '添加成功';
	            $return['data'] = [];
	            Output::instance()->object($return);
        	}else{
        		$return['code'] = 1;
	            $return['msg'] = '添加失败请重试';
	            $return['data'] = [];
	            Output::instance()->object($return);
        	}
        }
	}


	/**
     * APP启动图片删除
     * @param int $id   启动图片id
     */
	public function deleteSpalash($id){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_spalash = M('app_spalash');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        $time =  date('Y-m-d H:i:s',time());
    	$update_spalash['updateTime'] = $time;
    	$update_spalash['deleted'] = 1;
    	$update_result = $app_spalash->where("id=$id")->save($update_spalash);
    	/** 3.  判断删除是否成功 */        
    	if ($update_result !== false) {
    		$return['code'] = 0;
            $return['msg'] = '删除成功';
            $return['data'] = [];
            Output::instance()->object($return);
    	}else{
    		$return['code'] = 1;
            $return['msg'] = '删除失败请重试';
            $return['data'] = [];
            Output::instance()->object($return);
    	}
    }


	/**
     * APP启动图片后台查询
     * @param int $start   开始量
     * @param int $limit   偏移量
     */
	public function spalashList($start=0,$limit=10){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_spalash = M('app_spalash');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        $time =  date('Y-m-d H:i:s',time());
        $where_spalash['deleted'] = 0;  
    	$where_spalash['cid'] = $customerId; 	
    	//获取列表
    	$spalashList = $app_spalash->where($where_spalash)->limit($start,$limit)->select();
    	$spalashList = empty($spalashList) ? [] : $spalashList;
    	//获取总数
    	$total = $app_spalash->where($where_spalash)->count();
    	/** 4.  信息返回 */        
		$return['code'] = 0;
        $return['msg'] = 'app启动图片列表返回';
        $return['data'] = $spalashList;
        $return['total'] = $total;
        Output::instance()->object($return);
    }

    /**
     * 系统升级的后台设置
     * @param int $id   升级信息id
     * @param string $versionName 版本名称
     * @param int $versionNumber  版本号
     * @param int $isForceUpgrade 是否强制升级（0：不强制，1：强制
     * @param string $downloadingPath 下载路径
     * @param string $upgradeContentDescription 升级内容描述
     */
	public function updateUpgrade($id='',$versionName,$versionNumber,$isForceUpgrade,$downloadingPath,$upgradeContentDescription){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_upgrade = M('app_upgrade');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        $time =  date('Y-m-d H:i:s',time());
        if ($id){
        	$update_upgrade['versionName'] = $versionName;
        	$update_upgrade['versionNumber'] = $versionNumber;
        	$update_upgrade['isForceUpgrade'] = $isForceUpgrade;
        	$update_upgrade['downloadingPath'] = $downloadingPath;
        	$update_upgrade['upgradeContentDescription'] = $upgradeContentDescription;
        	$update_upgrade['updateTime'] = $time;
        	$update_result = $app_upgrade->where("id=$id")->save($update_upgrade);
        	/** 4.  判断编辑是否成功 */        
        	if ($update_result !== false) {
        		$return['code'] = 0;
	            $return['msg'] = '编辑成功';
	            $return['data'] = [];
	            Output::instance()->object($return);
        	}else{
        		$return['code'] = 1;
	            $return['msg'] = '编辑失败请重试';
	            $return['data'] = [];
	            Output::instance()->object($return);
        	}
        }else{
        	$add_upgrade['cid'] = $customerId;
        	$add_upgrade['versionName'] = $versionName;
        	$add_upgrade['versionNumber'] = $versionNumber;
        	$add_upgrade['isForceUpgrade'] = $isForceUpgrade;
        	$add_upgrade['downloadingPath'] = $downloadingPath;
        	$add_upgrade['upgradeContentDescription'] = $upgradeContentDescription;
        	$add_upgrade['createTime'] = $time;
        	$add_upgrade['updateTime'] = $time;
        	$add_result = $app_upgrade->add($add_upgrade);

        	/** 4.  判断添加是否成功 */
        	if ($add_result !== false) {
        		$return['code'] = 0;
	            $return['msg'] = '添加成功';
	            $return['data'] = [];
	            Output::instance()->object($return);
        	}else{
        		$return['code'] = 1;
	            $return['msg'] = '添加失败请重试';
	            $return['data'] = [];
	            Output::instance()->object($return);
        	}       	
        }
	}


	/**
     * APP系统升级信息删除
     * @param int $id   系统升级信息id
     */
	public function deleteUpgrade($id){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_upgrade = M('app_upgrade');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        $time =  date('Y-m-d H:i:s',time());
    	$update_upgrade['updateTime'] = $time;
    	$update_upgrade['deleted'] = 1;
    	$update_result = $app_upgrade->where("id=$id")->save($update_upgrade);
    	/** 3.  判断删除是否成功 */        
    	if ($update_result !== false) {
    		$return['code'] = 0;
            $return['msg'] = '删除成功';
            $return['data'] = [];
            Output::instance()->object($return);
    	}else{
    		$return['code'] = 1;
            $return['msg'] = '删除失败请重试';
            $return['data'] = [];
            Output::instance()->object($return);
    	}
    }


	/**
     * APP系统升级信息列表展示
     * @param int $start   开始量
     * @param int $limit   偏移量
     */
	public function upgradeInfoList($start=0,$limit=10){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_upgrade = M('app_upgrade');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        $where_upgrade['deleted'] = 0;
    	$where_upgrade['cid'] = $customerId;
    	//获取列表
    	$upgradeList = $app_upgrade->where($where_upgrade)->limit($start,$limit)->select();
    	$upgradeList = empty($upgradeList) ? [] : $upgradeList;
    	//获取总数
    	$total = $app_upgrade->where($where_upgrade)->count();
    	/** 4.  信息返回 */        
		$return['code'] = 0;
        $return['msg'] = '系统升级信息返回';
        $return['data'] = $upgradeList;
        $return['total'] = $total;
        Output::instance()->object($return);
}
	
    /**
     * APP日活量统计后台展示
     * @param int $start   开始量
     * @param int $limit   偏移量
     */
    public function dailyliveInfoList($start=0,$limit=10){
        /** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_dailylive = M('app_dailylive');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        //获取列表
        $where_dailylive['cid'] = $customerId;
        $dailyliveList = $app_dailylive->where($where_dailylive)->limit($start,$limit)->select();
        $dailyliveList = empty($dailyliveList) ? [] : $dailyliveList;
        //获取总数
        $total = $app_dailylive->count();
        /** 4.  信息返回 */        
        $return['code'] = 0;
        $return['msg'] = '系统日活量信息返回';
        $return['data'] = $dailyliveList;
        $return['total'] = $total;
        Output::instance()->object($return);
}
    
   
   

    	
}